<template>
  <div class="kpi-donut-panel">
    <div v-if="isLoading" class="loading-indicator">
      <div class="spinner"></div>
      <span>核心指标加载中...</span>
    </div>
    <template v-else>
      <div class="kpi-item" v-for="(item, index) in processedData" :key="item.title">
        <div class="chart-wrapper">
          <svg viewBox="0 0 100 100">
            <circle cx="50" cy="50" r="45" class="bg"></circle>
            <circle cx="50" cy="50" r="45" class="progress" :style="{ strokeDasharray: item.circumference, stroke: item.color }"></circle>
          </svg>
          <div class="value" :style="{ color: item.color }">{{ animatedValues[index] }}</div>
        </div>
        <div class="title">{{ item.title }}</div>
      </div>
    </template>
  </div>
</template>

<script setup>
import { computed, defineProps, ref, onMounted, watch } from 'vue';
import { useCountUp } from '@/composables/useCountUp';

const props = defineProps({
  data: {
    type: Array,
    required: true,
    default: () => []
  },
  isLoading: {
    type: Boolean,
    default: false,
  }
});

const getCircumference = (value, max) => {
    const radius = 45;
    const circumference = 2 * Math.PI * radius;
    if (max === 0) return `0 ${circumference}`;
    const progress = (value / max) * circumference;
    return `${progress} ${circumference}`;
};

const processedData = computed(() => {
    const values = props.data.map(item => Number(item.value) || 0);
    const maxValue = Math.max(...values, 1);
    return props.data.map((item, index) => ({
        ...item,
        circumference: getCircumference(values[index], maxValue)
    }));
});

// 创建一个数组来存储每个项目的动画值
const animatedValues = ref([]);

// 当数据变化时，更新动画值
watch(() => props.data, (newData) => {
    // 确保animatedValues数组长度与data相同
    if (animatedValues.value.length !== newData.length) {
        animatedValues.value = Array(newData.length).fill('0');
    }
    
    // 为每个数据项创建动画
    newData.forEach((item, index) => {
        const value = Number(item.value) || 0;
        const displayValue = useCountUp(() => value);
        
        // 监听displayValue的变化并更新animatedValues
        watch(displayValue, (newVal) => {
            animatedValues.value[index] = newVal;
        }, { immediate: true });
    });
}, { immediate: true, deep: true });

</script>

<style scoped>
.kpi-donut-panel {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 100%;
}

.loading-indicator {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  color: #aed4ff;
  font-size: 16px;
}

.spinner {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 4px solid rgba(0, 229, 255, 0.2);
  border-top-color: #00e5ff;
  animation: rotate 1s linear infinite;
  margin-bottom: 15px;
}

@keyframes rotate {
  100% {
    transform: rotate(360deg);
  }
}

.kpi-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}
.chart-wrapper {
    position: relative;
    width: 100px;
    height: 100px;
}
.chart-wrapper .value {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 24px;
    font-weight: bold;
}
svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}
circle.bg {
  fill: none;
  stroke: rgba(255, 255, 255, 0.1);
  stroke-width: 10;
}
circle.progress {
  fill: none;
  stroke-width: 10;
  stroke-linecap: round;
  transition: stroke-dasharray 0.5s ease;
}
.title {
    font-size: 16px;
    color: #aed4ff;
}
</style> 